﻿using DemountCIM.Framework;
using DemountCIM.Framework.DataBase;
using DemountCIM.Framework.PLC;
using DemountCIM.ViewHelper;
using DemountCIM.ViewModels;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using WpfUIDLL.WFControls;
using Path = System.Windows.Shapes.Path;

namespace DemountCIM.View
{
    /// <summary>
    /// OverView.xaml 的交互逻辑
    /// </summary>
    public partial class OverView : UserControl
    {
        OverViewModel model;
        DispatcherTimer dataUpdateTimer;

        private const string plateIDTitle = "晶托编码";

        private const string demounter1Step = "脱胶机1步骤：";
        private const string demounter2Step = "脱胶机2步骤：";
        private const string palletizingStep = "树脂板码垛步骤：";
        private const string removeGlueStep = "铲胶机步骤：";
        private const string cleaningStep = "清洗机步骤：";

        private Label Demounter1Info;                                   // 脱胶机1运行步骤
        private Label Demounter2Info;                                   // 脱胶机2运行步骤
        private Label PalletizingInfo;                                  // 树脂板码垛运行步骤
        private Label RemoveGlueInfo;                                   // 铲胶机运行步骤
        private Label CleaningInfo;                                     // 清洗机运行步骤
        private Label AMRCartInfo1;
        private Label AMRCartInfo2;
        private Label AMRCartInfo3;                                      // 树脂板上下料

        // 晶托编号
        private Label Demounter1PlateID;
        private Label Demounter2PlateID;
        private Label PalletizingPlateID;
        private Label RemoveGluePlateID;
        private Label[] CleaningPlateID;


        private Viewbox PalletizingReady;                               // 树脂板码垛出入箭头
        private Viewbox LoadPlate1Ready;                                // 上晶托1出入箭头
        private Viewbox LoadPlate2Ready;                                // 上晶托2出入箭头


        private Label[] PortAccessMode;                                 // 端口状态

        // 端口手自动状态
        AnoleCustomButton[] preAccessModeButton;

        public OverView()
        {
            InitializeComponent();

            model = new OverViewModel();

            DataContext = model;

            CleaningPlateID = new Label[2];

            preAccessModeButton = new AnoleCustomButton[3];

            InitControl();

            dataUpdateTimer = new DispatcherTimer();
            dataUpdateTimer.Interval = new TimeSpan(0, 0, 1);  //ms
            dataUpdateTimer.Tick += new EventHandler(tmr_timeout);
            dataUpdateTimer.IsEnabled = true;
            //dataUpdateTimer.Start();

        }

        ~OverView()
        {
            dataUpdateTimer.Stop();
        }

        private void InitControl()
        {
            #region 晶托初始化

            Demounter1PlateID = new Label();
            Demounter1PlateID.Background = Brushes.CornflowerBlue;
            Demounter1PlateID.Foreground = Brushes.DarkSlateGray;
            this.DeMountCanvas.Children.Add(Demounter1PlateID);

            Demounter2PlateID = new Label();
            Demounter2PlateID.Background = Brushes.CornflowerBlue;
            Demounter2PlateID.Foreground = Brushes.DarkSlateGray;
            this.DeMountCanvas.Children.Add(Demounter2PlateID);

            PalletizingPlateID = new Label();
            PalletizingPlateID.Background = Brushes.CornflowerBlue;
            PalletizingPlateID.Foreground = Brushes.DarkSlateGray;
            this.DeMountCanvas.Children.Add(PalletizingPlateID);

            RemoveGluePlateID = new Label();
            RemoveGluePlateID.Background = Brushes.CornflowerBlue;
            RemoveGluePlateID.Foreground = Brushes.DarkSlateGray;
            this.DeMountCanvas.Children.Add(RemoveGluePlateID);

            for (int i = 0; i < CleaningPlateID.Length; i++)
            {
                CleaningPlateID[i] = new Label();
                CleaningPlateID[i].Background = Brushes.CornflowerBlue;
                CleaningPlateID[i].Foreground = Brushes.DarkSlateGray;
                this.DeMountCanvas.Children.Add(CleaningPlateID[i]);
            }

            #endregion

            #region 运行步骤初始化

            Demounter1Info = new Label();
            Demounter1Info.Background = Brushes.IndianRed;
            Demounter1Info.Foreground = Brushes.White;
            this.DeMountCanvas.Children.Add(Demounter1Info);
            Demounter2Info = new Label();
            Demounter2Info.Background = Brushes.IndianRed;
            Demounter2Info.Foreground = Brushes.White;
            this.DeMountCanvas.Children.Add(Demounter2Info);
            PalletizingInfo = new Label();
            PalletizingInfo.Background = Brushes.SlateGray;
            PalletizingInfo.Foreground = Brushes.White;
            this.DeMountCanvas.Children.Add(PalletizingInfo);
            RemoveGlueInfo = new Label();
            RemoveGlueInfo.Background = Brushes.Gray;
            RemoveGlueInfo.Foreground = Brushes.White;
            this.DeMountCanvas.Children.Add(RemoveGlueInfo);
            CleaningInfo = new Label();
            CleaningInfo.Background = Brushes.SeaGreen;
            CleaningInfo.Foreground = Brushes.White;
            this.DeMountCanvas.Children.Add(CleaningInfo);

            #endregion

            #region AMR出入箭头

            PalletizingReady = new Viewbox();
            Path path = new Path();
            PalletizingReady.Child = path;
            this.DeMountCanvas.Children.Add(PalletizingReady);

            LoadPlate1Ready = new Viewbox();
            path = new Path();
            LoadPlate1Ready.Child = path;
            this.DeMountCanvas.Children.Add(LoadPlate1Ready);

            LoadPlate2Ready = new Viewbox();
            path = new Path();
            LoadPlate2Ready.Child = path;
            this.DeMountCanvas.Children.Add(LoadPlate2Ready);

            #endregion

            #region AMR调度步骤

            AMRCartInfo1 = new Label();
            AMRCartInfo1.Background = Brushes.CadetBlue;
            AMRCartInfo1.Foreground = Brushes.White;
            AMRCartInfo1.HorizontalContentAlignment = HorizontalAlignment.Center;
            this.DeMountCanvas.Children.Add(AMRCartInfo1);
            AMRCartInfo2 = new Label();
            AMRCartInfo2.Background = Brushes.CadetBlue;
            AMRCartInfo2.Foreground = Brushes.White;
            AMRCartInfo2.HorizontalContentAlignment = HorizontalAlignment.Center;
            this.DeMountCanvas.Children.Add(AMRCartInfo2);
            AMRCartInfo3 = new Label();
            AMRCartInfo3.Background = Brushes.CadetBlue;
            AMRCartInfo3.Foreground = Brushes.White;
            AMRCartInfo3.HorizontalContentAlignment = HorizontalAlignment.Center;
            this.DeMountCanvas.Children.Add(AMRCartInfo3);

            #endregion

            #region 端口状态

            PortAccessMode = new Label[3];
            for (int i = 0; i < PortAccessMode.Length; i++)
            {
                PortAccessMode[i] = new Label();
                this.DeMountCanvas.Children.Add(PortAccessMode[i]);
            }

            #endregion

        }

        void tmr_timeout(object sender, EventArgs e)
        {
            var ctrl = MachineCtrl.GetInstance();

            #region 晶托编号

            if (string.IsNullOrEmpty(ctrl.Demounter1PlateID))
            {
                Demounter1PlateID.Visibility = Visibility.Hidden;
            }
            else
            {
                Demounter1PlateID.Content = $"【{ctrl.Demounter1PlateID}】";
                Demounter1PlateID.Visibility = Visibility.Visible;
            }
            if (string.IsNullOrEmpty(ctrl.Demounter2PlateID))
            {
                Demounter2PlateID.Visibility = Visibility.Hidden;
            }
            else
            {
                Demounter2PlateID.Content = $"【{ctrl.Demounter2PlateID}】";
                Demounter2PlateID.Visibility = Visibility.Visible;
            }
            if (string.IsNullOrEmpty(ctrl.PalletizingPlateID))
            {
                PalletizingPlateID.Visibility = Visibility.Hidden;
            }
            else
            {
                PalletizingPlateID.Content = $"【{ctrl.PalletizingPlateID}】";
                PalletizingPlateID.Visibility = Visibility.Visible;
            }
            if (string.IsNullOrEmpty(ctrl.RemoveGluePlateID))
            {
                RemoveGluePlateID.Visibility = Visibility.Hidden;
            }
            else
            {
                RemoveGluePlateID.Content = $"【{ctrl.RemoveGluePlateID}】";
                RemoveGluePlateID.Visibility = Visibility.Visible;
            }


            if (!ctrl.PlateIDCleaningIn.IsEmpty)
            {
                string[] data = new string[2];
                var array = ctrl.PlateIDCleaningIn.ToArray();
                if (array.Length >= 2)
                {
                    data[0] = array[0];
                    data[1] = array[1];

                    for (int i = 0; i < CleaningPlateID.Length; i++)
                    {
                        if (string.IsNullOrEmpty(data[i]))
                        {
                            CleaningPlateID[i].Visibility = Visibility.Hidden;
                        }
                        else
                        {
                            CleaningPlateID[i].Content = $"【{data[i]}】";
                            CleaningPlateID[i].Visibility = Visibility.Visible;
                        }
                    }
                }


            }

            #endregion

            #region 树脂板和晶托上下料

            if (ctrl.AutoAMRBeamStep == MachineCtrl.AMRBeamSteps.AMRBeam_StartLoad)
            {
                (PalletizingReady.Child as Path).Fill = Brushes.Green;
                (PalletizingReady.Child as Path).Data = Application.Current.Resources["icon_out3"] as Geometry;
            }
            else if (ctrl.AutoAMRBeamStep == MachineCtrl.AMRBeamSteps.AMRBeam_StartUnload)
            {
                (PalletizingReady.Child as Path).Fill = Brushes.Red;
                (PalletizingReady.Child as Path).Data = Application.Current.Resources["icon_in3"] as Geometry;
            }

            if (ctrl.AutoAMRPlate1Step == MachineCtrl.AMRPlateSteps.AMRPlate_WaitLoad)
            {
                (LoadPlate1Ready.Child as Path).Fill = Brushes.Green;
                (LoadPlate1Ready.Child as Path).Data = Application.Current.Resources["icon_out1"] as Geometry;
            }
            else if (ctrl.AutoAMRPlate1Step == MachineCtrl.AMRPlateSteps.AMRPlate_WaitUnload)
            {
                (LoadPlate1Ready.Child as Path).Fill = Brushes.Red;
                (LoadPlate1Ready.Child as Path).Data = Application.Current.Resources["icon_in1"] as Geometry;
            }

            if (ctrl.AutoAMRPlate2Step == MachineCtrl.AMRPlateSteps.AMRPlate_WaitLoad)
            {
                (LoadPlate2Ready.Child as Path).Fill = Brushes.Green;
                (LoadPlate2Ready.Child as Path).Data = Application.Current.Resources["icon_out1"] as Geometry;
            }
            else if (ctrl.AutoAMRPlate2Step == MachineCtrl.AMRPlateSteps.AMRPlate_WaitUnload)
            {
                (LoadPlate2Ready.Child as Path).Fill = Brushes.Red;
                (LoadPlate2Ready.Child as Path).Data = Application.Current.Resources["icon_in1"] as Geometry;
            }

            AMRCartInfo1.Content = ctrl.AMRPlate1RunMsg;
            AMRCartInfo2.Content = ctrl.AMRPlate2RunMsg;
            AMRCartInfo3.Content = ctrl.AMRBeamRunMsg;


            #endregion

            #region 步骤信息

            Demounter1Info.Content = $"{demounter1Step}{ctrl.Demounter1RunMsg}";
            Demounter2Info.Content = $"{demounter2Step}{ctrl.Demounter2RunMsg}";
            PalletizingInfo.Content = $"{palletizingStep}{ctrl.PalletizingRunMsg}";
            RemoveGlueInfo.Content = $"{removeGlueStep}{ctrl.RemoveGlueRunMsg}";
            CleaningInfo.Content = $"{cleaningStep}{ctrl.CleaningRunMsg}";

            #endregion

            #region 端口状态



            #endregion

            #region 更新链接状态

            plcStatus1.Fill = ctrl.PlcIsConnected() ? Brushes.LightGreen : Brushes.IndianRed;
            rfidStatus1.Fill = ctrl.RfidIsConnected(0) ? Brushes.LightGreen : Brushes.IndianRed;
            rfidStatus2.Fill = ctrl.RfidIsConnected(3) ? Brushes.LightGreen : Brushes.IndianRed;
            databaseStatus.Fill = DataBaseContainer.GetInstance().GetConnection() ? Brushes.LightGreen : Brushes.IndianRed;


            #endregion
        }

        private void DeMountCanvas_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // Y轴
            var frame1 = this.DeMountCanvas.ActualHeight / 1000;
            // X轴
            var frame2 = this.DeMountCanvas.ActualWidth / 1000;

            // 运行步骤=====================================
            Canvas.SetTop(Demounter1Info, frame1 * 900);
            Canvas.SetLeft(Demounter1Info, frame2 * 280);

            Canvas.SetTop(Demounter2Info, frame1 * 230);
            Canvas.SetLeft(Demounter2Info, frame2 * 280);

            Canvas.SetTop(PalletizingInfo, frame1 * 780);
            Canvas.SetLeft(PalletizingInfo, frame2 * 430);

            Canvas.SetTop(RemoveGlueInfo, frame1 * 625);
            Canvas.SetLeft(RemoveGlueInfo, frame2 * 540);

            Canvas.SetTop(CleaningInfo, frame1 * 600);
            Canvas.SetLeft(CleaningInfo, frame2 * 700);

            #region 上晶托

            LoadPlate1Ready.Height = frame1 * 100;
            Canvas.SetTop(LoadPlate1Ready, frame1 * 660);
            Canvas.SetLeft(LoadPlate1Ready, frame2 * 70);

            AMRCartInfo1.Width = frame2 * 110;
            Canvas.SetTop(AMRCartInfo1, frame1 * 900);
            Canvas.SetLeft(AMRCartInfo1, frame2 * 10);

            LoadPlate2Ready.Height = frame1 * 100;
            Canvas.SetTop(LoadPlate2Ready, frame1 * 385);
            Canvas.SetLeft(LoadPlate2Ready, frame2 * 70);

            AMRCartInfo2.Width = frame2 * 110;
            Canvas.SetTop(AMRCartInfo2, frame1 * 230);
            Canvas.SetLeft(AMRCartInfo2, frame2 * 10);



            #endregion

            #region 脱胶机

            var plateLeft = frame2 * 300;
            // 晶托1
            Canvas.SetTop(Demounter1PlateID, frame1 * 700);
            Canvas.SetLeft(Demounter1PlateID, plateLeft);
            // 晶托2
            Canvas.SetTop(Demounter2PlateID, frame1 * 410);
            Canvas.SetLeft(Demounter2PlateID, plateLeft);

            #endregion

            var plateTop = frame1 * 700;
            #region 树脂板码垛

            // 晶托3
            Canvas.SetTop(PalletizingPlateID, plateTop);
            Canvas.SetLeft(PalletizingPlateID, frame2 * 430);

            PalletizingReady.Height = frame1 * 100;
            Canvas.SetTop(PalletizingReady, frame1 * 360);
            Canvas.SetLeft(PalletizingReady, frame2 * 470);

            AMRCartInfo3.Width = frame2 * 110;
            Canvas.SetTop(AMRCartInfo3, frame1 * 320);
            Canvas.SetLeft(AMRCartInfo3, frame2 * 430);

            #endregion



            // 铲胶机========================================
            Canvas.SetTop(RemoveGluePlateID, plateTop);
            Canvas.SetLeft(RemoveGluePlateID, frame2 * 550);

            // 清洗机========================================

            Canvas.SetTop(CleaningPlateID[0], plateTop);
            Canvas.SetLeft(CleaningPlateID[0], frame2 * 680);
            Canvas.SetTop(CleaningPlateID[1], plateTop);
            Canvas.SetLeft(CleaningPlateID[1], frame2 * 820);


            // 数据写入=====================================
            // 晶托6
            //Canvas.SetTop(PlateID[5], plateTop);
            //Canvas.SetLeft(PlateID[5], frame2 * 50);


        }

        #region 系统状态

        private void eqpOffline_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesControlStateOffline();
        }

        private void localOnline_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesControlStateLocal();
        }

        private void remoteOnline_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesControlStateRemote();
        }

        #endregion

        private void plate1PortManual_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesPortAccessModeChange(0, "B", Framework.SECSGEM.PortAccessMode.ManualMode);

            var button = sender as AnoleCustomButton;

            if (preAccessModeButton[0] != null)
            {
                preAccessModeButton[0].Background = Brushes.Yellow;
                preAccessModeButton[0].Foreground = Brushes.Black;
            }
            preAccessModeButton[0] = button;

            preAccessModeButton[0].Background = Brushes.Green;
            preAccessModeButton[0].Foreground = Brushes.White;
        }

        private void plate1PortAuto_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesPortAccessModeChange(0, "B", Framework.SECSGEM.PortAccessMode.AutoMode);


            var button = sender as AnoleCustomButton;

            if (preAccessModeButton[0] != null)
            {

                preAccessModeButton[0].Background = Brushes.Yellow;
                preAccessModeButton[0].Foreground = Brushes.Black;
            }
            preAccessModeButton[0] = button;

            preAccessModeButton[0].Background = Brushes.Green;
            preAccessModeButton[0].Foreground = Brushes.White;
        }

        private void plate2PortManual_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesPortAccessModeChange(1, "B", Framework.SECSGEM.PortAccessMode.ManualMode);


            var button = sender as AnoleCustomButton;

            if (preAccessModeButton[1] != null)
            {

                preAccessModeButton[1].Background = Brushes.Yellow;
                preAccessModeButton[1].Foreground = Brushes.Black;
            }
            preAccessModeButton[1] = button;

            preAccessModeButton[1].Background = Brushes.Green;
            preAccessModeButton[1].Foreground = Brushes.White;
        }

        private void plate2PortAuto_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesPortAccessModeChange(1, "B", Framework.SECSGEM.PortAccessMode.AutoMode);

            var button = sender as AnoleCustomButton;

            if (preAccessModeButton[1] != null)
            {

                preAccessModeButton[1].Background = Brushes.Yellow;
                preAccessModeButton[1].Foreground = Brushes.Black;
            }
            preAccessModeButton[1] = button;

            preAccessModeButton[1].Background = Brushes.Green;
            preAccessModeButton[1].Foreground = Brushes.White;
        }

        private void beamPortManual_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesPortAccessModeChange(2, "B", Framework.SECSGEM.PortAccessMode.ManualMode);

            var button = sender as AnoleCustomButton;

            if (preAccessModeButton[2] != null)
            {

                preAccessModeButton[2].Background = Brushes.Yellow;
                preAccessModeButton[2].Foreground = Brushes.Black;
            }
            preAccessModeButton[2] = button;

            preAccessModeButton[2].Background = Brushes.Green;
            preAccessModeButton[2].Foreground = Brushes.White;
        }

        private void beamPortAuto_Click(object sender, RoutedEventArgs e)
        {
            MachineCtrl.GetInstance().MesPortAccessModeChange(2, "B", Framework.SECSGEM.PortAccessMode.AutoMode);

            var button = sender as AnoleCustomButton;

            if (preAccessModeButton[2] != null)
            {

                preAccessModeButton[2].Background = Brushes.Yellow;
                preAccessModeButton[2].Foreground = Brushes.Black;
            }
            preAccessModeButton[2] = button;

            preAccessModeButton[2].Background = Brushes.Green;
            preAccessModeButton[2].Foreground = Brushes.White;
        }

    }
}
